//
//  HMEmoticonToolBarView.swift
//  WeiBo
//
//  Created by Feng on 2017/6/14.
//  Copyright © 2017年 feng. All rights reserved.
//

import UIKit

enum HMEmoticonToolBarButtonType: Int
{
    //最近使用
    case recent = 1000
    //默认
    case normal = 1001
    //emoji
    case emoji = 1002
    //浪小花
    case lxh = 1003
}

class HMEmoticonToolBarView: UIStackView {

    //记录上次被选中的按钮
    var lastButton: UIButton?
    
    //按钮的回调
    var emoticonToolBarCallBack: ((HMEmoticonToolBarButtonType) -> ())?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI()
    {
        //设置默认选中"默认(第二个)"button
        
        
        //设置stackView
        self.axis = .horizontal
        self.distribution = .fillEqually
        
        addChildButton(imageName: "compose_emotion_table_left", titleName: "最近", buttonType: .recent)
        addChildButton(imageName: "compose_emotion_table_mid", titleName: "默认", buttonType: .normal)
        addChildButton(imageName: "compose_emotion_table_mid", titleName: "emoji", buttonType: .emoji)
        addChildButton(imageName: "compose_emotion_table_right", titleName: "浪小花", buttonType: .lxh)
    }
    
    private func addChildButton(imageName: String, titleName: String, buttonType:HMEmoticonToolBarButtonType)
    {
        let button = UIButton()

        button.setBackgroundImage(UIImage(named:"\(imageName)_normal"), for: .normal)
        button.setBackgroundImage(UIImage(named:"\(imageName)_selected"), for: .selected)
        button.setTitle(titleName, for: .normal)
        button.setTitleColor(UIColor.darkGray, for: .selected)
        button.setTitleColor(UIColor.white, for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        
        button.adjustsImageWhenHighlighted = false
        button.tag = buttonType.rawValue
        
        button.addTarget(self, action: #selector(toolBarButtonAction(btn:)), for: .touchUpInside)
        
        addArrangedSubview(button)
        
        if buttonType == .normal
        {
            lastButton?.isSelected = false
            button.isSelected = true
            lastButton = button
        }
    }
    
    @objc private func toolBarButtonAction(btn: UIButton)
    {
        if lastButton == btn
        {
            return
        }
        
        lastButton?.isSelected = false
        
        btn.isSelected = true
        lastButton = btn
        
        //执行闭包
        let type = HMEmoticonToolBarButtonType(rawValue: btn.tag)
        emoticonToolBarCallBack?(type!)
    }
    
    //根据上面collectionView的section选中对应按钮
    func selectedButtonWithSection(section: Int)
    {
        //  根据tag获取对应的子视图
        //  如果tag是0，获取的视图不是子视图，是当前视图自己
        //  注意点: 设置视图的tag的是不建议设置为0
        let button = self.viewWithTag(section + 1000) as! UIButton
        
        if button == lastButton
        {
            return
        }
        
        lastButton?.isSelected = false
        button.isSelected = true
        lastButton = button
    }
}
